﻿$(document).ready(function(){
    imgSize();
    imgSize2();
    mainTopHeight();
    $(window).scroll(function(){
        navChange();
    });
})
var priceModel;  //价格组
var priceNumberGroup=0;//当前数量组
function downpdf(pdfurl,pdfid){

//    $(".base_info p .downpdf-en").text("准备文件...");
//    $(".base_info p .downpdf-en").attr("href","javascript:void(0);");
//    $(".base_info p .downpdf-en").addClass("startdownpdf");
//    $(".base_info p .downpdf-en").removeClass("downpdf");

    $("#main .loadpdf").attr("href","javascript:void(0);");
    $("#main .loadpdf").find("img").attr("src",webpath+"/img_mo/search/wait.gif").css("width","100px!important");
    var parent=$("#main .loadpdf");
    mydownpdf(pdfurl,pdfid,pdfinforesultEvent,parent);
    //mydownpdf(pdfurl,pdfid,pdfinforesultEvent);
}
function pdfinforesultEvent(msg,pdfid,obj){
    if(msg.type=="error"){
        $(obj).find("span").text("暂无");
        $(obj).find("img").attr("src",webpath+"/img_mo/search/downpdf.png").css("opacity","0.3");
        $(obj).attr("onclick","");
    }else if(msg.type=="nofile"){
        $(obj).find("span").text("暂无");
        $(obj).find("img").attr("src",webpath+"/img_mo/search/downpdf.png").css("opacity","0.3");
        $(obj).attr("onclick","");
    }else{
        $(obj).find("img").attr("src",webpath+"/img_mo/search/downpdf_end.png");
        $(obj).find("span").text("下载");

        $(obj).attr("target","_blank");
        $(obj).attr("href",msg.url);
        $(obj).attr("onclick","");
    }
}

function priceAjax(no,manu_r,requestCount){
    $.ajax({
        type: "get",
        url: "/price/detail/search",
        data:"&no=" + encodeURIComponent(no)+"&manu="+encodeURIComponent(manu_r)+ "&count="+requestCount+"&date=" + new Date().getTime(),
        dataType: "json",
        success: function(msg) {
            if(msg.length==0){
                requestCount++;
                if(requestCount<=3){
                    setTimeout(function(){priceAjax(no,manu_r,requestCount);},3000);
                }else{
                    $("#main .tdprice_k_em").html("暂无现货");
                }
            }else{
                priceShow(msg);
                $(".scrollbar .scrollbox:eq(0)").addClass("comeOn");
                //findMinPrice();

            }
        },
        error:function(XMLHttpRequest,textStatus,errorThrown){
        }
    });
}
function priceShow(model){

    priceModel=model;

    var result=model.priceList;
    priceSort(result);

    if(result.length>0){
        //先显示最低价格(在主模块中)
        var _minprice_v=999999.0;
        var _minprice_s="";
        var _minprice_u="";
        var _minprice_c=0;
        var _total_c=0;
        for(var v=0;v<result.length;v++){
            _total_c=_total_c+ result[v][0].c;
            for(var p=0;p<result[v].length;p++){
                if(model.numberRanges[0]==result[v][p].k){
                    if(_minprice_v>result[v][p].v){
                        _minprice_v= result[v][p].v;
                        _minprice_s= result[v][p].s;
                        _minprice_u= result[v][p].u;
                        _minprice_c= result[v][p].c;
                    }
                }
            }
        }

        //main
        var _priceShow="";
        var _priceAll=_minprice_v.toFixed(2);
        var _prices=_priceAll.split(".");
        if(_prices.length==2){
            _priceShow="<span style='font-size:1.1em'>"+_prices[0]+"</span><span style='font-size:0.8em'>."+_prices[1]+"</span>";
        }else{
            _priceShow="<span style='font-size:1.1em'></span><span style='font-size:0.8em'>.00</span>";
        }
        $("#main .priceshow").html("<span style='font-size:0.8em'>"+_minprice_u+"</span>"+_priceShow).css("display","block");
        $("#main .tdprice_k_em").html("现货 "+_total_c+"个");
        //main2
        $(".allBox .allBox_price").html("<p>价格：<span >最低单价</span><span style='color:#ff9d46'>"+_minprice_u+" "+_minprice_v.toFixed(2)+"</span><span>，来自供应商</span><span style='color:#2689ff'>"+$.trim(_minprice_s)+"</span></p>");
        $(".allBox .allBox_kucun").html("<p>库存：<span>现货共<span style='color:#03a82d'>"+_total_c+"</span>个，其中<span style='color:#129735'>"+_minprice_c+"</span>个来自供应商<span style='color:#2689ff'>"+$.trim(_minprice_s)+"</span></span></p>");

        //showall
        var str=new StringBuffer();
        //str.append("<div class=\"base_price\">");
        //起定量
        str.append("<div class=\"qiding\">");
        str.append("     <span style='color:#434343;padding:0;float:left;width:100%;'>起订量</span>");
        str.append("     <div colspan=\"3\" class=\"price_title_group\">");
        str.append("        <div class=\"makeCenter\">");
        str.append("            <div class=\"scrollstart\">"+model.numberRanges[0]+"</div>");
        str.append("            <div class=\"scrollbar\">");


        var _tdsize=model.numberRanges.length>5?5:model.numberRanges.length;
        var wucha=120;
        if(model.numberRanges[_tdsize-1]>=1000){
            wucha=150;
        }
        var mainwidth=$("#main").width()-wucha;
        var mainbtnleft=mainwidth/(_tdsize);

        for(var v=0;v<_tdsize;v++){
            if(v==0){
                str.append("        <div class=\"scrollbox scrollboxSelect scrollboxfirst\" onclick='priceSelect(this,"+v+")'><a  href='javascript:void(0);' style='display:none;'>"+model.numberRanges[v]+"</a></div>");
            }else{
                str.append("        <div class=\"scrollbox\" onclick='priceSelect(this,"+v+")' style='margin-left:"+mainbtnleft+"px !important'><a  href='javascript:void(0);' style='display:none;'>"+model.numberRanges[v]+"</a></div>");
            }
        }
        str.append("                <div class=\"scrollBar\"></div>");
        str.append("                <div class=\"scrollBar2\" style='width:0px;'></div>");
        str.append("            </div>");
        str.append("            <div class=\"scrollend\">"+model.numberRanges[_tdsize-1]+"</div>");
        str.append("        </div>");
        str.append("    </div>");
        str.append("</div>")
        //str.append("<h2>价格/库存</h2>");
        str.append("<table style=\"width:100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");

        //起定量
//        str.append("<tr>");
//        str.append("    <td colspan=\"3\" class=\"price_title_group\"><span style='color:#0d83dd;padding:0;float:left;'>起订量</span>");
//        str.append("        <div class=\"scrollbar\">");
//        var _tdsize=model.numberRanges.length>5?5:model.numberRanges.length;
//        for(var v=0;v<_tdsize;v++){
//            if(v==0){
//                str.append("<div class=\"scrollbox scrollboxSelect scrollboxfirst\" onclick='priceSelect(this,"+v+")'><a  href='javascript:void(0);'>"+model.numberRanges[v]+"</a></div>");
//            }else{
//                str.append("<div class=\"scrollbox\" onclick='priceSelect(this,"+v+")'><a  href='javascript:void(0);'>"+model.numberRanges[v]+"</a></div>");
//            }
//        }
//        str.append("        </div>");
//        str.append("    </td>");
//        str.append("</tr>");

        str.append("<tr>");
        str.append("    <td class='tdone'>供应商</td>");
        str.append("    <td class='tdtwo tdfive'>库存</td>");
        str.append("    <td class='tdthree tdfour'>价格</td>");
        str.append("</tr>");
        str.append("<tr>");
        str.append("<td colspan='3' ><img class='solid' src='"+webpath+"/img_mo/detail/solid.png' /></td>");
        str.append("</tr>");
        //结果
        for(var v=0;v<result.length;v++){
            var rowstr=new StringBuffer();
            rowstr.append("<tr>");
            rowstr.append(" <td class='tdone'>"+$.trim(result[v][0].s)+"</td>");
            rowstr.append(" <td class='tdtwo'>"+$.trim(result[v][0].c)+"</td>");

            //只显示第1组价格
            var _have=false;
            for(var p=0;p<result[v].length;p++){
                if(model.numberRanges[0]==result[v][p].k){
                    rowstr.append("<td class='tdthree'>"+result[v][p].u+" <span>"+result[v][p].v.toFixed(2)+"</span></td>");
                    _have=true;
                    break;
                }
            }
            if(!_have){
                rowstr.append("<td class='tdthree'>&nbsp;</td>");
            }

            rowstr.append("</tr>");

            str.append(rowstr.toString());
        }

        str.append("</table>");
        //str.append("</div>");

        $(".base_price").html(str.toString());

        //改变头部，增加一列        canshu
        if($(".mainBottom .canshu").length==1){
            //存在参数
            $(".mainBottom .introduction").add(".mainBottom .canshu").css("width","");
            $(".mainBottom .thisprice").add(".mainBottom .xian2").css("display","block");
            $(".mainBottom .xian1").css("left","");
        }else{
            //无参数
            $(".mainBottom .introduction").add(".mainBottom .thisprice").css("width","50%");
            $(".mainBottom .thisprice").css("display","block");
            $(".mainBottom .xian1").css("left","50%").css("display","block");
        }

    }
}

//按价格排序
function priceSort(result){

    for(var v=0;v<result.length-1;v++){
        for(var v2=0;v2<result.length-1-v;v2++){
            var _j1=9999;
            for(var p1=0;p1<result[v2].length;p1++){
                if(priceModel.numberRanges[priceNumberGroup]==result[v2][p1].k){
                    _j1= result[v2][p1].v;
                    break;
                }
            }

            var _j2=9999;
            for(var p2=0;p2<result[v2+1].length;p2++){
                if(priceModel.numberRanges[priceNumberGroup]==result[v2+1][p2].k){
                    _j2= result[v2+1][p2].v;
                    break;
                }
            }
            if(_j1>_j2){
                var _obj=result[v2];
                result[v2]=result[v2+1];
                result[v2+1]=_obj;
            }

        }
    }
}
function priceSelect(obj,id){

    if(priceNumberGroup==id){

    }else{

        //改变状态
        var _total=$(".main2 .scrollbar .scrollbox").length;
        $(".main2 .scrollbar .scrollBar2").css("width",(id/(_total-1)*100)+"%");
        $(".main2 .scrollbar .scrollbox:lt("+(id+1)+")").addClass("scrollboxSelect");
        $(".main2 .scrollbar .scrollbox:gt("+((id-1)<0?0:(id-1))+")").removeClass("scrollboxSelect");
        //$(".main2 .scrollbar .scrollbox:eq("+id+")").addClass("scrollboxSelect");

        $(".scrollbar .scrollbox:eq("+priceNumberGroup+")").removeClass("comeOn");
        $(".scrollbar .scrollbox:eq("+id+")").addClass("comeOn");

        $(".scrollbar .scrollbox:eq("+priceNumberGroup+") a").css("display","none");
        if(id!=0 && id!=(_total-1)){
            $(".scrollbar .scrollbox:eq("+id+") a").css("display","block");
        }

        //$(".price_title_group a:eq("+priceNumberGroup+")").parent().removeClass("scrollboxSelect");
        priceNumberGroup=id;
       // $(".price_title_group a:eq("+priceNumberGroup+")").parent().addClass("scrollboxSelect");

        var result=priceModel.priceList;
        priceSort(result);

        for(var v=0;v<result.length;v++){
            $(".base_price table tr:eq("+(v+2)+") td:eq(0) ").html($.trim(result[v][0].s));
            $(".base_price table tr:eq("+(v+2)+") td:eq(1) ").html($.trim(result[v][0].c));
            //只显示第X组价格
            var _have=false;
            for(var p=0;p<result[v].length;p++){
                if(priceModel.numberRanges[priceNumberGroup]==result[v][p].k){

                    $(".base_price table tr:eq("+(v+2)+") td:eq(2) ").html(result[v][p].u+" <span>"+result[v][p].v.toFixed(2)+"</span>");
                    _have=true;
                    break;
                }
            }
            if(!_have){
                $(".base_price table tr:eq("+(v+2)+") td:eq(2) ").html("&nbsp;");
            }
        }
    }
}

//只改动价格
function priceSelect_old(obj,id){
    if(priceNumberGroup==id){

    }else{
        $(".price_title_group a:eq("+priceNumberGroup+")").parent().removeClass("scrollboxSelect");
        priceNumberGroup=id;
        $(".price_title_group a:eq("+priceNumberGroup+")").parent().addClass("scrollboxSelect");

        var result=priceModel.priceList;

        for(var v=0;v<result.length;v++){

            //只显示第X组价格
            var _have=false;
            for(var p=0;p<result[v].length;p++){
                if(priceModel.numberRanges[priceNumberGroup]==result[v][p].k){

                    $(".base_price table tr:eq("+(v+2)+") td:eq(2) ").html(result[v][p].u+" <span>"+result[v][p].v.toFixed(2)+"</span>");
                    _have=true;
                    break;
                }
            }
            if(!_have){
                $(".base_price table tr:eq("+(v+2)+") td:eq(2) ").html("&nbsp;");
            }

        }
        findMinPrice();
    }
}
//查找最低价格
function findMinPrice(){

    $(".base_price table tr:gt(1)").each(function(index){
        $(this).find("td:eq(2)").css("color","");
        $(this).find("td:eq(2) span").css("font-weight","");
    });

    //给出每1列最低价格
    var trlength=$(".base_price table tr").length;
    var tdlength=$(".base_price table tr td").length;
    for(var _td=2;_td<tdlength;_td++){
        var _minp=999999.0;
        for(var _tr=2;_tr<trlength;_tr++){
            var _cup=parseFloat($(".base_price table tr:eq("+_tr+") td:eq("+_td+") span").text());
            if(_cup!=NaN){
                if(_cup<_minp){
                    _minp=_cup;
                }
            }
        }

        for(var _tr=2;_tr<trlength;_tr++){
            var _cup=parseFloat($(".base_price table tr:eq("+_tr+") td:eq("+_td+") span").text());
            if(_cup!=NaN){
                if(_cup==_minp){
                    $(".base_price table tr:eq("+_tr+") td:eq("+_td+") ").css("color","#eb8c1b");
                    $(".base_price table tr:eq("+_tr+") td:eq("+_td+") span").css("font-weight","bold");
                    break;
                }
            }
        }
    }
}

function imgSize(){
    $("#main li .left").each(function(index){
        $(this).find("div:eq(0)").css("height",$(this).find("div:eq(0)").css("width"));
    });
}

function imgSize2(){
    $(".imgBox span").each(function(index){
        $(this).css("height",$(this).css("width"));
    });
}
function mainTopHeight(){
    var _h=$(".navbar-top3").height()+$("#main").height()-30;
    $(".main2").css("margin-top",_h+"px");
}
var _currentbtngo=0;
var _btngorun=false;
function btngo(obj){
    _btngorun=true;
    if(_currentbtngo==obj){
        _btngorun=false;
       return;
    }
    $(".mainBottom a:eq("+_currentbtngo+") span").removeClass("mainBottom_a_span_select");
    $(".mainBottom a:eq("+obj+") span").addClass("mainBottom_a_span_select");
    _currentbtngo=obj;

    var _v;
    var _h=$(".navbar-top3").height();
    //alert(_h);
    if(obj==0){
        _v=$(".main2 .introduction");
    }else if(obj==1){
        _v=$(".main2 .base_price");
    }else if(obj==2){
        _v=$(".main2 .base_paramter");
    }
    $("html,body").animate({scrollTop:(_v.offset().top-_h)});
    _btngorun=false;
}

function  navChange(){
    return;
    //$(".load_pdf").text($(".main2 .introduction").offset().top).css("color","#fff");
    var marginBot=$(window).scrollTop();
//    if(document.documentElement.scrollTop){
//        marginBot=document.documentElement.scrollHeight-(document.documentElement.scrollTop+document.body.scrollTop)-document.documentElement.clientHeight;
//    }else{
//        marginBot=document.body.scrollHeight-document.body.scrollTop-document.documentElement.clientHeight;
//    }
    var _h=$(".navbar-top3").height()+$("#main").height()+19;

    //$(".load_pdf").text(_h+" "+marginBot).css("color","#fff");
    //return;

    marginBot=marginBot+_h+50;

    var obj=-1;
    if($(".main2 .base_paramter").offset().top<marginBot){
        obj=2;
    }else if($(".main2 .base_price").offset().top<marginBot){
        obj=1;
    }else if($(".main2 .introduction").offset().top<marginBot){
        obj=0;
    }
//    var _h=$(".navbar-top3").height()+$("#main").height()+19;
//
//    var obj=-1;
//    if($(".main2 .introduction").offset().top>_h){
//        obj=0;
//    }else if($(".main2 .base_price").offset().top>_h){
//        obj=1;
//    }else if($(".main2 .base_paramter").offset().top>_h){
//        obj=2;
//    }
    if(obj>=0){
        if(_currentbtngo==obj){
            return;
        }
        $(".mainBottom a:eq("+_currentbtngo+") span").removeClass("mainBottom_a_span_select");
        $(".mainBottom a:eq("+obj+") span").addClass("mainBottom_a_span_select");
        _currentbtngo=obj;
    }
}
